//
//  ECUCatalogController.m
//  Edu_iOS
//
//  Created by apple on 2017/9/23.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#import "ECUCatalogController.h"

#import "CYUITVDSD_base.h"
#import "CYUITVDSDBridge.h"

#import "ECUCatalogModel.h"//model

#import "ECUFirstHeadCell.h"//cell
#import "ECUSecondHeadCell.h"
#import "ECUThirdHeadCell.h"




extern CGFloat TABBAR_FOOTER_HEIGHT;

@interface ECUCatalogController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray <__kindof ECUCatalogModel *> *dataArray;

@end

@implementation ECUCatalogController
@synthesize tableView = _tableView;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)addSubviews{
    [super addSubviews];
    [self initDataSource];
    [self.view addSubview:self.tableView];
    
}

-(void)initDataSource{
    NSArray *netData = @[
                         @{
                             @"text":@"一年级数学",
                             @"submodels":@[
                                     @{
                                         @"text":@"第一章：加减法运算加减法运算",
                                         @"submodels":@[
                                                 @{
                                                     @"text":@"第一节：加法运算",
                                                     @"url" :@"123",
                                                     },
                                                 @{
                                                     @"text":@"第二节：减法运算",
                                                     },
                                                 @{
                                                     @"text":@"第三节：混合运算",
                                                     }
                                                 ]
                                         },
                                     @{
                                         @"text":@"第二章：乘除运算",
                                         @"submodels":@[
                                                 @{
                                                     @"text":@"第一节：乘法运算",
                                                     },
                                                 @{
                                                     @"text":@"第二节",
                                                     },
                                                 @{
                                                     @"text":@"第三节",
                                                     }
                                                 ]
                                         },
                                     @{
                                         @"text":@"第三章：混合运算",
                                         @"submodels":@[
                                                 @{
                                                     @"text":@"第一节：乘法运算",
                                                     },
                                                 @{
                                                     @"text":@"第二节",
                                                     },
                                                 @{
                                                     @"text":@"第三节",
                                                     }
                                                 ]
                                         }
                                     ]
                             },
                         @{
                             @"text":@"第二册",
                             @"submodels":@[
                                     @{
                                         @"text":@"第一章",
                                         @"submodels":@[
                                                 @{
                                                     @"text":@"第一节",
                                                     },
                                                 @{
                                                     @"text":@"第二节",
                                                     },
                                                 @{
                                                     @"text":@"第三节",
                                                     }
                                                 ]
                                         },
                                     @{
                                         @"text":@"第二章",
                                         @"submodels":@[
                                                 @{
                                                     @"text":@"第一节",
                                                     },
                                                 @{
                                                     @"text":@"第二节",
                                                     },
                                                 @{
                                                     @"text":@"第三节",
                                                     }
                                                 ]
                                         }
                                     ]
                             },
                         ];
    
    self.dataArray = [NSMutableArray new];
    for (int i = 0; i < netData.count; i++) {
        ECUCatalogModel *foldCellModel = [ECUCatalogModel modelWithDic:(NSDictionary *)netData[i]];
        [self.dataArray addObject:foldCellModel];
    }
    
    //添加level属性 进行分级
    for (int i = 0 ; i < self.dataArray.count; i++) {
        ECUCatalogModel *model_one = self.dataArray[i];
        model_one.level = [NSString stringWithFormat:@"0"];
        
        NSMutableArray *arr_one = model_one.submodels;
        for (int j = 0; j < arr_one.count; j++) {
            ECUCatalogModel *model_two = arr_one[j];
            model_two.level = [NSString stringWithFormat:@"1"];
            
            model_two.levelOneCount = arr_one.count;//每层level为1的总数量
            model_two.levelOneWitch = j;//每层level为1的序号
            
            NSMutableArray *arr_two = model_two.submodels;
            for (int k = 0; k < arr_two.count; k++) {
                ECUCatalogModel *model_three = arr_two[k];
                model_three.level = [NSString stringWithFormat:@"2"];
            }
        }
    }
}

-(void)tableViewDidScroll:(tableViewDidScrollBlock)scrollBlock{
    self.scrollBlock = scrollBlock;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.scrollBlock(scrollView.contentOffset.y);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ECUCatalogModel *foldCellModel = self.dataArray[indexPath.row];
    
    //在此区分cell格式
    if ([foldCellModel.level isEqualToString:@"0"]) {
        static NSString *identifier = @"ECUFirstHeadCell";
        ECUFirstHeadCell *firstCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!firstCell) {
            firstCell = [[ECUFirstHeadCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        [firstCell setDateOfCellWithModel:foldCellModel];
        return firstCell;
    }else if([foldCellModel.level isEqualToString:@"1"]){
        static NSString *identifier = @"ECUSecondHeadCell";
        ECUSecondHeadCell *secondCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!secondCell) {
            secondCell = [[ECUSecondHeadCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        [secondCell setDateOfCellWithModel:foldCellModel];
        return secondCell;
    }else{
        static NSString *identifier = @"ECUThirdHeadCell";
        ECUThirdHeadCell *thirdCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!thirdCell) {
            thirdCell = [[ECUThirdHeadCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        [thirdCell setDateOfCellWithModel:foldCellModel];
        return thirdCell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ECUCatalogModel *didSelectFoldCellModel = self.dataArray[indexPath.row];
    
    [tableView beginUpdates];
    if (didSelectFoldCellModel.belowCount == 0) {//展开经过的方法
        
        //Data
        NSArray *submodels = [didSelectFoldCellModel open];
        NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:((NSRange){indexPath.row + 1,submodels.count})];
        [self.dataArray insertObjects:submodels atIndexes:indexes];
        
        //Rows
        NSMutableArray *indexPaths = [NSMutableArray new];
        for (int i = 0; i < submodels.count; i++) {
            NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow:(indexPath.row + 1 + i) inSection:indexPath.section];
            [indexPaths addObject:insertIndexPath];
        }
        
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:(indexPath.row) inSection:indexPath.section];
        
        if ([didSelectFoldCellModel.level isEqualToString:@"0"]) {//第一级cell 改变箭头方向
            
            ECUFirstHeadCell *cell = (ECUFirstHeadCell *)[tableView cellForRowAtIndexPath:newIndexPath];
            [cell setArrowIconImageWithModel:didSelectFoldCellModel];
        }else if ([didSelectFoldCellModel.level isEqualToString:@"1"]) {//第二级cell中 最后一个cell 下部竖线显示与否的设置
            ECUSecondHeadCell *cell = (ECUSecondHeadCell *)[tableView cellForRowAtIndexPath:newIndexPath];
            [cell setBottomLineShowOrHiddenWithModel:didSelectFoldCellModel];
            [cell setArrowIconImageWithModel:didSelectFoldCellModel];

        }else if ([didSelectFoldCellModel.level isEqualToString:@"2"]) {//只有为最后一级时，进行改变值 //只有在上一级展开的情况下，才会点击播放
            if ([self.delegate respondsToSelector:@selector(didSelectOfCellWithModel:)]) {
                [self.delegate didSelectOfCellWithModel:didSelectFoldCellModel];
            }
            
            ECUThirdHeadCell *cell = (ECUThirdHeadCell *)[tableView cellForRowAtIndexPath:newIndexPath];
            [cell didSelectOfCellWithLearnStatus:didSelectFoldCellModel];//改变学习状态
        }
        
        [tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
        DLog(@"%@",didSelectFoldCellModel.text);
        
    }else {//收缩经过的方法
        
        //Data
        NSArray *submodels = [self.dataArray subarrayWithRange:((NSRange){indexPath.row + 1,didSelectFoldCellModel.belowCount})];
        [didSelectFoldCellModel closeWithSubmodels:submodels];
        [self.dataArray  removeObjectsInArray:submodels];
        
        //Rows
        NSMutableArray *indexPaths = [NSMutableArray new];
        for (int i = 0; i < submodels.count; i++) {
            NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow:(indexPath.row + 1 + i) inSection:indexPath.section];
            [indexPaths addObject:insertIndexPath];
        }
        
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:(indexPath.row) inSection:indexPath.section];
        
        if ([didSelectFoldCellModel.level isEqualToString:@"0"]) {//第一级cell 改变箭头方向
            ECUFirstHeadCell *cell = (ECUFirstHeadCell *)[tableView cellForRowAtIndexPath:newIndexPath];
            [cell setArrowIconImageWithModel:didSelectFoldCellModel];
        }else if ([didSelectFoldCellModel.level isEqualToString:@"1"]) {//第二级cell中 最后一个cell 下部竖线显示与否的设置
            ECUSecondHeadCell *cell = (ECUSecondHeadCell *)[tableView cellForRowAtIndexPath:newIndexPath];
            [cell setBottomLineShowOrHiddenWithModel:didSelectFoldCellModel];
            [cell setArrowIconImageWithModel:didSelectFoldCellModel];
        }
        
        [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }
    [tableView endUpdates];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ECUCatalogModel *foldCellModel = self.dataArray[indexPath.row];
    
    if ([foldCellModel.level isEqualToString:@"0"] || [foldCellModel.level isEqualToString:@"1"]) {
        return 150.f*KProportion1080_Heigh;
    }else {
        return 180.f*KProportion1080_Heigh;
    }
    
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-TABBAR_TITLE_HEIGHT-64 - TABBAR_FOOTER_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
    }
    return _tableView;
}

@end
